'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
  async index() {

    const {
      ctx,
    } = this;
    ctx.body = '你好啊';
  }
  async getImage() {
    const idx = this.ctx.query.idx;
    const res = await this.ctx.curl('https://cn.bing.com/HPImageArchive.aspx?format=js&n=1&pid=hp&idx=' + idx);
    // console.log(res.data);
    this.ctx.body = res.data;
  }
  // 登录
  async login() {
    if (!this.ctx.query.user_name) {
      // eslint-disable-next-line no-return-assign
      return this.ctx.body = {
        status: 400,
        mwssage: '查无此人',
      };
    }
    const result = await this.app.mysql.get('blog_users', this.ctx.query);
    console.log(result)
    if (!result) {
      // eslint-disable-next-line no-return-assign
      this.ctx.body = {
        status: 400,
        mwssage: '查无此人',
      };
    } else {
      const token = this.app.jwt.sign({
        foo: 'bar',
      }, this.app.config.jwt.secret);
      result.token = token;
      this.ctx.body = result;
    }
  }
  // 获取文章,可以根据id查询,否则就返回所有的文章
  async getArticleList() {
    let result;
    if (this.ctx.query.id) {
      // console.log(1)
      result = await this.app.mysql.query(` select * from blog_articles where article_id = ` + this.ctx.query.id);
    } else {
      // console.log(2)
      result = await this.app.mysql.query(` select * from blog_articles `);
    }
    // console.log(3)
    this.ctx.body = result;
  }
  // 获取所有分类以及分类下的文章名
  async getSorts() {
    // 先获取所有分类
    let result = await this.app.mysql.query(` select * from blog_sorts`);
    // 根据分类查询文章
    const res = await this.app.mysql.query(`select * from blog_articles left join blog_set_artitle_sort on blog_articles.article_id = blog_set_artitle_sort.article_id`)
    // console.log(result)
    res.forEach(item => {
      // console.log(item);
      result = this.service.home.sortPJ(result, item);
    });
    this.ctx.body = result;
  }
  // 获取评论，需要传入文章的id
  async getComments() {
    console.log(this.ctx.query.article_id);
    const result = await this.app.mysql.query(` select * from blog_comments where article_id = ` + this.ctx.query.article_id);
    let parent = [];
    const child = [];
    result.forEach(item => {
      if (!item.parent_comment_id) {
        parent.push(item);
      } else {
        child.push(item);
      }
    })
    // eslint-disable-next-line array-callback-return
    child.map(item => {
      parent = this.service.home.commentPJ(parent, item);
    });
    console.log(parent);
    this.ctx.body = parent;
  }
  // 存入评论,get方法，需要传入user_id,article_id,comment_content,parent_comment_id(可选)
  async submitComment() {
    const query = {
      article_id: 10008,
      user_id: 1,
      comment_content: 'lalalawowowow',
      comment_date: new Date(),
      comment_like_count: 0,
    };
    const result = await this.app.mysql.insert('blog_comments', query)
    console.log(result);
  }
  // 存入文章，get，传入user_id,article_title,article_content其他的自己补成0
  async submitArticle() {
    const query = {
      user_id: 1,
      article_title: '第三个',
      article_content: 'adafdfgdfhfgjghkjghkkyrhdgdfsdfafaasdafdf',
      article_views: 0,
      article_comment_count: 0,
      article_date: new Date(),
      article_like_count: 0,
      article_labels: null,
    }
    const result = await this.app.mysql.insert('blog_articles', query);
    console.log(result);
    this.ctx.body = result;
  }
  // 文章信息更新/编辑，get,把文章的信息传递过来，传递什么就改什么。article_id必选，如果content为空就是删除。
  async updateArticle() {
    console.log(1)
    const query = {
      article_content: 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbdadasfdggfgsdasd',
    }
    const result = await this.app.mysql.update('blog_articles', query, {
      where: {
        article_id: 10003,
        user_id: 1,
        article_title: '第三个',
        article_content: 'adafdfgdfhfgjghkjghkkyrhdgdfsdfafaasdafdf',
        article_views: 0,
        article_comment_count: 0,
        article_date: new Date(),
        article_like_count: 0,
        article_labels: null,
      },
    });
    console.log(result);
    this.ctx.body = result;
  }
  // 添加分类,分为一级或者二级分类
  async addSort() {
    const query = {
      sort_name: 'vue',
      sort_alias: 'vue',
      sort_description: 0,
      parent_sort_id: 190,
    }
    const result = await this.app.mysql.insert('blog_sorts', query);
    console.log(result);
    this.ctx.body = result;
  }
  // 分类修改需要获取id,如果为空，就是删除
  async editSort() {
    const query = {
      sort_name: 'vue',
      sort_alias: 'vuevue',
      sort_description: 0,
      parent_sort_id: 190,
    }
    const result = await this.app.mysql.update('blog_sorts', query, {
      where: {
        sort_id: 1903,
      },
    });
    console.log(result);
    this.ctx.body = result;
  }

}

module.exports = HomeController;